package alexattar.gdxtest.screens;

import java.util.Locale;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;

import alexattar.gdxtest.DefaultActorListener;
import alexattar.gdxtest.GdxTestGame;
import alexattar.gdxtest.Profile;
import alexattar.gdxtest.services.SoundManager.TyrianSound;

public class OptionsScreen extends AbstractScreen {

	private Label volumeValue;
	
	public OptionsScreen(GdxTestGame game) {
		super(game);
	}
	
	@Override
	public void show() {
		super.show();
        Profile profile = game.getProfileManager().retrieveProfile();

        Skin skin = super.getSkin();
		// retrieve the default table actor
        Table table = new Table(getSkin());
        table.setFillParent(true);
        stage.addActor(table);
        
     // create the labels widgets
        final CheckBox soundEffectsCheckbox = new CheckBox("", skin );
        soundEffectsCheckbox.setChecked( game.getPreferences().isSoundEffectsEnabled() );
        soundEffectsCheckbox.addListener( new ClickListener() {
            @Override
            public void clicked(
                InputEvent event,                
                float x,
                float y )
            {
                boolean enabled = soundEffectsCheckbox.isChecked();
				game.getSoundManager().play(TyrianSound.CLICK);
				game.getSoundManager().setEnabled(enabled);
                game.getPreferences().setSoundEffectsEnabled( enabled );
            }
        } );
        table.row();
        table.add("Sound Effects:  ");
        table.add(soundEffectsCheckbox).colspan(2).left();

        final CheckBox musicCheckbox = new CheckBox("", skin);
        musicCheckbox.setChecked( game.getPreferences().isMusicEnabled() );
        musicCheckbox.addListener( new ClickListener() {
            @Override
            public void clicked(
            	InputEvent event,
                float x,
                float y )
            {
                boolean enabled = musicCheckbox.isChecked();
				game.getSoundManager().play(TyrianSound.CLICK);
				game.getMusicManager().setEnabled(enabled);
                game.getPreferences().setMusicEnabled( enabled );
            }
        } );
        table.row();
        table.add("Music:  ");
        table.add(musicCheckbox).colspan(2).left();

        // range is [0.0,1.0]; step is 0.1f
        Slider volumeSlider = new Slider( 0f, 1f, 0.1f, false, skin );
        volumeSlider.setValue( game.getPreferences().getVolume() );
        volumeSlider.addListener( new ChangeListener() {
            @Override
            public void changed(
                ChangeEvent even,
                Actor actor )
            {
            	float volume = ((Slider) actor).getValue();
                game.getPreferences().setVolume(volume);
				game.getMusicManager().setVolume(volume);
				game.getSoundManager().setVolume(volume);
                game.getSoundManager().play(TyrianSound.CLICK);
                updateVolumeLabel();
            }
        } );        

        volumeValue = new Label("", skin );
        updateVolumeLabel();

        table.row();
        table.add("Volume:  ");
        table.add(volumeSlider);
        table.add(volumeValue).width(40);

        // register the back button
        TextButton backButton = new TextButton( "Back to main menu", skin );
        backButton.addListener( new DefaultActorListener() {
            @Override
            public void touchUp(
                InputEvent event,
                float x, float y,
                int pointer, int button)
            {
            	super.touchUp(event, x, y, pointer, button);
				game.getSoundManager().play(TyrianSound.CLICK);
                game.setScreen( new MenuScreen(game) );
            }
        } );
        table.row();
        table.add(backButton).size(250,60).colspan(3);
	}
	
	private void updateVolumeLabel() {
		float volume = game.getPreferences().getVolume() * 100;
		volumeValue.setText(String.format(Locale.CANADA, "%1.0f%%", volume));
	}

}
